package com.yuanlp.mobileplayer.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.yuanlp.mobileplayer.R;
import com.yuanlp.mobileplayer.base.BasePager;
import com.yuanlp.mobileplayer.pager.AudioPager;
import com.yuanlp.mobileplayer.pager.NetAudioPager;
import com.yuanlp.mobileplayer.pager.NetVideoPager;
import com.yuanlp.mobileplayer.pager.VideoPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面
 * Created by 原立鹏 on 2017/7/12.
 */

public class MainActivity extends FragmentActivity{

    private FrameLayout fl_main_content;
    private RadioGroup rg_bottom_tag;

    /**
     * 本地页面集合
     */
    public static List<BasePager> basepagers;

    /**
     * 选中的位置
     */
    public static int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        fl_main_content= (FrameLayout) findViewById(R.id.fl_main_content);
        rg_bottom_tag= (RadioGroup) findViewById(R.id.rg_bottom_tag);


        basepagers=new ArrayList<BasePager>();
        basepagers.add(new VideoPager(this)); //本地视频  位置是0
        basepagers.add(new AudioPager(this));  //本地音乐 位置是1
        basepagers.add(new NetVideoPager(this));  //网络视频 位置是2
        basepagers.add(new NetAudioPager(this));  //网络音乐 位置是3


        //在这里设置radiogroup的监听
        rg_bottom_tag.setOnCheckedChangeListener(new myOnCheckedChangeListener());
        rg_bottom_tag.check(R.id.rb_video);  //默认选中主页的本地视频

    }

    class myOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{

        /**
         * <p>Called when the checked radio button has changed. When the
         * selection is cleared, checkedId is -1.</p>
         *
         * @param group     the group in which the checked radio button has changed
         * @param checkedId the unique identifier of the newly checked radio button
         */
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){

                default:
                    position=0;
                    break;
                case R.id.rb_audio:  //选中的是本地音乐
                    position=1;
                    break;
                case R.id.rb_video:  //选中的是本地视频
                    position=0;
                    break;
                case R.id.rb_net_audio:  //选中的是网络音乐
                    position=3;
                    break;
                case R.id.rb_net_video:  //选中的是网络视频
                    position=2;
                    break;
            }

            //把创建的页面放到Fragment中
            setFragment();
        }

        private void setFragment() {
            //Fragment的步骤
            //1、获取FragmentManager
            FragmentManager fragmentManager = getSupportFragmentManager();
            //2、开启事务
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            //3、替换Fragement
            //transaction.replace(R.id.fl_main_content,new Fragment());
            transaction.replace(R.id.fl_main_content,new myFragment());
            //4、提交事务
            transaction.commit();
        }
    }

    public static class myFragment extends Fragment{
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            BasePager b1=getBasePager();
            if (b1!=null){
                return b1.rootview;
            }
            return null;
        }
    }

    public static BasePager getBasePager() {
        BasePager basepager=basepagers.get(position);
        if (basepager!=null&&!basepager.isinitData){

            basepager.initData();  //初始化页面的数据，调用各个页面类里的initData()方法
            basepager.isinitData=true;
            return basepager;
        }
        return basepager;
    }
}
